// 登录页
var sign = function(req, res) {
    return res.render('account/sign', {
        next: req.query.next || 'back'
    });

}

// 登录信息检测
var ckUserNameAndPassword = function(req, res, next) {
    if (!req.body.username) {
        res.flash('error', '用户名不能为空');
        return res.redirect('back');

    } else if (!req.body.password) {
        res.flash('error', '密码不能为空');
        return res.redirect('back');

    } else {
        return next();
    }

}


// 登录信息验证
var signin = function(req, res, next) {
    var db = require('../models');

    db.model('User').authenticate(req.body.username, req.body.password, function(err, user) {
        if (err) {
            req.flash('error', '用户不存在或密码错误! ');
            return res.redirect('back');

        } else {
            req.flash('success', '登录成功');
            login(req, user);

            return next();
        }

    });

}

// 登录
var login = function(req, user) {
    req.session.user = user.toObject();

    return req;
}

// ajax登录
var ajaxSignin = function(req, res) {
    var flash = {};

    var db = require('../models');

    db.model('User').authenticate({
        username: req.body.username,
        password: req.body.password
    }, function(err, user) {
        if (err) {
            flash = {
                type: 'error',
                msg: '用户不存在或密码错误！'
            }

        } else {
            login(req, user);

            flash = {
                type: 'success',
                msg: '登录成功'
            }

        }

        return res.json(flash);

    });
}




// 登出
var logout = function(req, res, next) {
    delete req.session.user;
    return next();

}


// 注册表单页
var signup = function(req, res) {

    return res.render('account/signup', {
        title: '注册'
    });

}

// 注册两次密码验证
var checkRegPwd = function(req, res, next) {
    if (req.body.password !== req.body.password2) {
        req.flash('error', '两次输入的密码不一致，请重试输入！');
        return res.redirect('back');
    } else {
        return next();

    }
}


// 用户注册
var register = function(req, res, next) {
    var db = require('../models');

    db.model('User').createOne({
        username: req.body.username,
        password: req.body.password
    }, function(err, user) {
        if (err) {
            req.flash('error', err.message);
            return res.redirect('back');

        } else {
            login(req, user);
            req.flash('success', '注册成功！');

            return next();

        }

    });

}


// 用户名占用检测
var checkUser = function(req, res) {
    var db = require('../models');

    db.model('User').findByUserName(req.query.username, function(err, user) {
        if (err || user) {
            return res.send('false');

        } else {
            return res.send('true');

        }

    });


}


// 登录检查
var auth = function(req, res, next) {
    if (req.session.user) {
        return next();

    } else {
        return res.redirect('/account/sign/');

    }
}


// 跳转
var redirect = function(req, res) {
    return res.redirect(req.query.next || 'back');

}


exports.sign = sign;

exports.signup = signup;
exports.register = register;

exports.checkRegPwd = checkRegPwd;
exports.checkUser = checkUser;

exports.ckUserNameAndPassword = ckUserNameAndPassword;
exports.signin = signin;

exports.ajaxSignin = ajaxSignin;

exports.logout = logout;


exports.auth = auth;
exports.redirect = redirect;
